package com.controller;

import com.dto.PwdDTO;
import com.dto.TypeDTO;
import com.service.LoginServiceImpl;
import com.service.TypeServiceImpl;
import com.util.Result;
import com.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author linyukun
 * @date 2022/7/7 18:42
 */
@RequestMapping("type")
@RestController
public class TypeController {

    @Autowired
    private LoginServiceImpl loginServiceImpl;

    @Autowired
    private TypeServiceImpl typeServiceImpl;

    /**
     * 获取图书类型的列表（所有的）
     * @author linyukun
     * @date 2022-07-07 10:36
     */
    @RequestMapping(value="/getAllTypeList",method = RequestMethod.POST)
    public Result getAllTypeList(HttpServletRequest request){
        try{
            loginServiceImpl.isLogin();
            return ResultUtil.returnDataSuccess(typeServiceImpl.getAllTypeList());
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 获取图书类型的列表
     * @author linyukun
     * @date 2022-07-07 10:36
     */
    @RequestMapping(value="/getTypeList",method = RequestMethod.POST)
    public Result getTypeList(HttpServletRequest request, @RequestBody TypeDTO dto){
        try{
            loginServiceImpl.isLogin();
            return ResultUtil.returnDataSuccess(typeServiceImpl.getTypeList(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 删除图书类型
     * @author linyukun
     * @date 2022-07-07 10:36
     */
    @RequestMapping(value="/delTypeById",method = RequestMethod.POST)
    public Result delTypeById(HttpServletRequest request, @RequestBody TypeDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setDelUser(userName);
            typeServiceImpl.delTypeById(dto);
            return ResultUtil.returnDataSuccess("1");
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 添加图书类型
     * @author linyukun
     * @date 2022-07-07 10:36
     */
    @RequestMapping(value="/addType",method = RequestMethod.POST)
    public Result addType(HttpServletRequest request, @RequestBody TypeDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setCreateUser(userName);
            typeServiceImpl.addType(dto);
            return ResultUtil.returnDataSuccess("1");
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }


    /**
     * 修改图书类型
     * @author linyukun
     * @date 2022-07-07 10:36
     */
    @RequestMapping(value="/editTypeById",method = RequestMethod.POST)
    public Result editTypeById(HttpServletRequest request, @RequestBody TypeDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setEditUser(userName);
            typeServiceImpl.editTypeById(dto);
            return ResultUtil.returnDataSuccess("1");
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }
}
